package fitnesshub.controller;

import fitnesshub.pojo.dto.AddEquipmentDTO;
import fitnesshub.pojo.dto.EquipmentPageQueryDTO;
import fitnesshub.pojo.entity.Equipment;
import fitnesshub.result.PageResult;
import fitnesshub.result.Result;
import fitnesshub.service.EquipmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 设备管理
 */
@RestController
@RequestMapping("/admin/equipment")
@Slf4j
public class EquipmentController {

    @Autowired
    private EquipmentService equipmentService;

    /**
     * 设备分页
     *
     * @param equipmentPageQueryDTO 分页查询参数对象
     * @return 包含分页结果的Result对象
     */
    @GetMapping("/page")
    public Result<PageResult> page(EquipmentPageQueryDTO equipmentPageQueryDTO) {
        log.info("设备分页查询，参数：{}", equipmentPageQueryDTO);
        PageResult pageResult = equipmentService.page(equipmentPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询设备
     *
     * @param equipmentId 设备ID
     * @return 包含对应设备信息的Result对象
     */

    @GetMapping("/{equipmentId}")
    public Result<Equipment> getById(@PathVariable Integer equipmentId) {
        log.info("根据id:{},查询设备", equipmentId);
        Equipment equipment = equipmentService.getById(equipmentId);
        return Result.success(equipment);
    }

    /**
     * 新增设备
     *
     * @param addEquipmentDTO 新增设备的数据传输对象
     * @return 表示操作成功的Result对象
     */
    @PostMapping
    public Result addEquipment(@RequestBody AddEquipmentDTO addEquipmentDTO) {
        log.info("新增设备:{}", addEquipmentDTO);
        equipmentService.addEquipment(addEquipmentDTO);
        return Result.success();
    }

    /**
     * 编辑设备
     *
     * @param equipment 包含编辑后设备信息的实体对象
     * @return 表示操作成功的Result对象
     */
    @PutMapping
    public Result editEquipment(@RequestBody Equipment equipment) {
        log.info("编辑设备:{}", equipment);
        equipmentService.editEquipment(equipment);
        return Result.success();
    }

    /**
     * 根据id删除设备
     *
     * @param equipmentId 要删除设备的ID
     * @return 表示操作成功的Result对象
     */
    @DeleteMapping("/{equipmentId}")
    public Result deleteById(@PathVariable Integer equipmentId) {
        log.info("删除设备,id:{}", equipmentId);
        equipmentService.deleteById(equipmentId);
        return Result.success();
    }
}